package com.caldremch.datastore;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.graphics.Color;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.caldremch.storage.CalCore;

import java.io.ByteArrayInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Arrays;


public class MainActivity extends AppCompatActivity {




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        assignViews();
        initData();

        MyLog.e("deviceid="+CalCore.getDeviceId());


    }

    private void initData() {
        tvDyRegInfo.setText(CalCore.getTestStr());
        tvSignInfo.setText(EncryptUtils.getMD5(getSignInfo()));


    }

    private byte[] getSignInfo() {
        try {
            @SuppressLint("PackageManagerGetSignatures") PackageInfo packageInfo = this.getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNATURES);
            Signature[] signatures = packageInfo.signatures;
            /******************获取sha1值******************/
            //获取签名数组的第一位
            Signature sign = signatures[0];
            CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
            byte[] signByte = sign.toByteArray();
            ByteArrayInputStream bytein = new ByteArrayInputStream(signByte);
            //获取x509证书
            X509Certificate cert = (X509Certificate) certificateFactory.generateCertificate(bytein);
            //获取证书发行者SHA1
            MessageDigest sha1 = MessageDigest.getInstance("SHA1");
            byte[] certByte = cert.getEncoded();
            byte[] bs = sha1.digest(certByte);
            Log.e("caldremch", "sha1(byte) = "+ Arrays.toString(bs));
            // C9C59636571FC8C0BEDE25C957AA84875C233961 正确
            Log.e("caldremch", "sha1(hex) = "+EncryptUtils.bytes2Hex(bs));
            Log.e("caldremch", "md5(signature) = "+EncryptUtils.getMD5(signatures[0].toByteArray()));
            return signatures[0].toByteArray();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } catch (CertificateException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return null;
    }

    private TextView tvDyRegInfo;
    private TextView tvSignInfo;

    private void assignViews() {
        tvDyRegInfo = (TextView) findViewById(R.id.tv_dy_reg_info);
        tvSignInfo = (TextView) findViewById(R.id.tv_sign_info);
    }

    public void openCamera(View view) {

        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(i, 10);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);



    }
}
